/*
 * OpenParts
 * A dynamic-loading components framework for GWT
 * Copyright (C) 2011 Christophe Bouthier  [chris{AT}binary-gastronome{DOT}fr]
 *
 * This work is partially based on work I have done at INRIA (http://www.inria.fr) 
 * in the context of the Qualipso European Project (http://qualipso.org/),
 * The work done at INRIA is Copyright (C) 2006-2011 INRIA
 *
 * This work is distributed under the LGPL version 3
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation. See the GNU
 * Lesser General Public License in LGPL.txt for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package fr.openparts.OpenParts.test.gwt.tests;

import com.google.gwt.junit.client.GWTTestCase;

import fr.openparts.OpenParts.client.OPBindingAction;
import fr.openparts.OpenParts.client.OPBindingResource;

/**
 * GWT tests for OPBindingResource.
 * 
 * @author Christophe Bouthier [chris{AT}binary-gastronome{DOT}fr]
 * @creation.date 31 December 2009
 */
public class OPBindingResourceTestGwt extends GWTTestCase {

    private static final String RESOURCE_NAME = "dummyResource";
    private final static String ACTION_NAME = "dummyAction";

    private OPBindingResource resource;
    private OPBindingAction action;

    /**
     * Return the name of the tested GWT Module.
     * 
     * @see com.google.gwt.junit.client.GWTTestCase#getModuleName()
     */
    @Override
    public final String getModuleName() {
        return "fr.openparts.OpenParts.test.gwt.TestOpenParts";
    }

    /**
     * Called at the start of each test to set up the resource.
     * 
     * @see com.google.gwt.junit.client.GWTTestCase#gwtSetUp()
     */
    @Override
    protected void gwtSetUp() throws Exception {
        super.gwtSetUp();
        resource = OPBindingResource.resourceFactory(RESOURCE_NAME);
        action = OPBindingAction.actionFactory(ACTION_NAME);
    }

    /**
     * Test creating an OPBindingResource. Should get something not null.
     */
    public final void testCreatingOPBindingResource() {
        assertNotNull("Binding resource shouldn't be null", resource);
    }

    /**
     * Test creating an OPBindingResource with a null resource name. Should raise an assertion error.
     */
    public final void testCreatingOPBindingResourceWithNullResourceName() {
        if (assertionsAreEnabled()) {
            try {
                OPBindingResource.resourceFactory(null);
                fail("OPBindingResource should raise an assertion error when creating a resource with null resource name.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test getting the resource name. Should get RESOURCE_NAME.
     */
    public final void testGettingResourceName() {
        final String resourceName = resource.getResourceName();
        assertNotNull(" Resource name shouldn't be null", resourceName);
        assertEquals("Resource name should be dummyResource", RESOURCE_NAME, resourceName);
    }

    /**
     * Test getting all names of binded actions. Should get the corresponding array.
     */
    public final void testGettingActionsNames() {
        resource.bindAction(action);
        final OPBindingAction action2 = OPBindingAction.actionFactory(ACTION_NAME + "2");
        resource.bindAction(action2);
        final String[] actionsNames = resource.getActionsNames();
        assertNotNull("Names array shouldn't be null", actionsNames);
        assertEquals("Names array should contains only 2 elements", 2, actionsNames.length);
        boolean actionName1Found = false;
        boolean actionName2Found = false;
        for (String actionName : actionsNames) {
            if (actionName.equals(ACTION_NAME)) {
                actionName1Found = true;
            } else if (actionName.equals(ACTION_NAME + "2")) {
                actionName2Found = true;
            } else {
                fail("Names array contains an unknown action name: " + actionName);
            }
        }
        assertTrue("action name 1 should have been found", actionName1Found);
        assertTrue("action name 2 should have been found", actionName2Found);
    }

    /**
     * Test getting all names of binded resources without resources binded. Should get an empty but not null array.
     */
    public final void testGettingActionsNamesWithoutActions() {
        final String[] actionsNames = resource.getActionsNames();
        assertNotNull("Names array shouldn't be null", actionsNames);
        assertEquals("Names array should be empty", 0, actionsNames.length);
    }

    /**
     * Test binding an action. Shouldn't get an exception.
     */
    public final void testBindingAction() {
        resource.bindAction(action);
        assertNotNull(resource.getAction(ACTION_NAME));
    }

    /**
     * Test binding a null action. Should raise an assertion error.
     */
    public final void testBindingNullAction() {
        if (assertionsAreEnabled()) {
            try {
                resource.bindAction(null);
                fail("OPBindingResource should raise an assertion error when binding a null action.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test binding an already binded action. Shouldn't get an exception.
     */
    public final void testBindingAlreadyBindedAction() {
        resource.bindAction(action);
        final OPBindingAction action2 = OPBindingAction.actionFactory(ACTION_NAME);
        assertNotSame("actions shouldn't be the same", action, action2);
        resource.bindAction(action2);
        assertEquals("Should return the lastest binded action", action2, resource.getAction(ACTION_NAME));
    }

    /**
     * Test getting a binding action. Should get the binded action.
     */
    public final void testGettingBindedAction() {
        resource.bindAction(action);
        final OPBindingAction a = resource.getAction(ACTION_NAME);
        assertNotNull("action shouldn't be null", a);
        assertEquals("Should return the binded action", action, a);
        ;
    }

    /**
     * Test getting a binded action without binding the action first. Should get null.
     */
    public final void testGettingActionWithoutBindingFirst() {
        assertNull("action should be null if not binded first", resource.getAction(ACTION_NAME));
    }

    /**
     * Test getting a binded action by passing null as name. Should raise an assertion error.
     */
    public final void testGettingNullAction() {
        if (assertionsAreEnabled()) {
            resource.bindAction(action);
            try {
                resource.getAction(null);
                fail("OPBindingResource should raise an assertion error when getting an action by passing a null name.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Test getting an inexistent action. Should get null.
     */
    public final void testGettingInexistentAction() {
        resource.bindAction(action);
        assertNull("action should be null if inexistant name is given", resource.getAction("INEXISTENT_ACTION"));
    }

    /**
     * Check if assertions are enabled.
     * 
     * @return true if assertions are enabled, false otherwise
     */
    private final boolean assertionsAreEnabled() {
        try {
            assert false;
            return false;
        } catch (AssertionError e) {
            return true;
        }
    }

}
